home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
The Best of MacTutor - S…e Code for Volumes 1 to 5
/
The Best of MacTutor - Source Code for Volume 1-5 (Wayzata Technology)(6031)(1990).bin
/
Source Code
/
#44 (May 89)
/
DA.sit
/
UselessDA.c
< prev
next >
Wrap
C/C++ Source or Header
|
1988-06-04
|
4KB
|
209 lines
#include <MacTypes.h>
#include <MemoryMgr.h>
#include <Quickdraw.h>
#include <WindowMgr.h>
#include <EventMgr.h>
#include <OSUtil.h>
#include <ResourceMgr.h>
#include <ToolboxUtil.h>
#include <DeviceMgr.h>
#define NULL (0L)
#define drvrOpen 0
#define drvrPrime 1
#define drvrControl 2
#define drvrStatus 3
#define drvrClose 4
#define subidProc 0
enum {
irtnOpenWindow = 0,
irtnDrawWindow,
irtnKillWindow
};
extern short main(CntrlParam*,DCtlPtr,short);
extern short DAOpen(CntrlParam*,DCtlPtr);
extern short DAClose(CntrlParam*,DCtlPtr);
extern WindowPtr OpenWindow(char*,short,short,short,short);
extern void DrawWindow(WindowPtr,char*);
extern void KillWindow(WindowPtr);
extern Boolean FLoadProc(short,Ptr*);
extern void UnloadProc(Ptr);
Ptr pProc;
short rsidBase;
#define JumpToProc(pPROC,irtn,cwArgs) \
asm { \
unlk a6 /* get rid of LSC's frame */ \
movea.l sp,a0 /* address of first argument */ \
lea -8(sp),sp /* make room on the stack */ \
movea.l sp,a1 /* bottom of stack */ \
move.w #(cwArgs)+1,d0 /* (# of words to copy)-1 */ \
/* copy rtn addr and arguments */ \
LCopyAnother: \
move.w (a0)+,(a1)+ /* move a word down */ \
dbra d0,@LCopyAnother /* done when --d0 == -1 */ \
move.l a4,(a1)+ /* copy our a4 into storage */ \
move.l (sp)+,(a1) /* copy our rtn addr just behind it */ \
\
pea @LReturnToCaller /* push return address for procedure*/ \
move.w #irtn,-(sp) /* push routine selector */ \
move.l pPROC,-(sp) /* push address of PROC */ \
beq.s @LHandleError /* jump if passed a NULL pPROC */ \
LJumpToProc: \
rts /* jump to proc (sleazy trick) */ \
\
LHandleError: \
dc.w 0xA9FF /* let Macsbug "handle" the error */ \
lea 10(sp),sp /* fix up the stack */ \
\
LReturnToCaller: \
lea (2*(cwArgs))(sp),a0 /* get address of our storage space */ \
movea.l (a0)+,a4 /* restore our a4 */ \
movea.l (a0),a0 /* get our return address */ \
lea 8(sp),sp /* fix up the stack */ \
jmp (a0) /* return to sender (address known) */ \
}
short
main(piopb, pdce, drvrRoutine)
CntrlParam *piopb;
DCtlPtr pdce;
short drvrRoutine;
{
GrafPtr pgpSav;
short valRtn;
if (pdce->dCtlStorage == NULL) {
if (drvrRoutine == 0) { /* open, but no data */
SysBeep(3);
CloseDriver(pdce->dCtlRefNum);
}
return(0);
}
GetPort ( &pgpSav );
switch (drvrRoutine) {
case drvrOpen:
valRtn = DAOpen(piopb,pdce);
break;
case drvrControl:
case drvrPrime:
case drvrStatus:
valRtn = 0;
break;
case drvrClose:
valRtn = DAClose(piopb,pdce);
break;
}
SetPort ( pgpSav );
return valRtn;
} /* main */
short
DAOpen(piopb,pdce)
CntrlParam *piopb; /* pointer to parameter block */
DCtlPtr pdce; /* the device control entry */
{
rsidBase = 0xC000 | (~pdce->dCtlRefNum<<5);
if ( !FLoadProc ( 0, &pProc ) ) {
SysBeep(2);
CloseDriver(pdce->dCtlRefNum);
return 0;
}
/* Using hard-wired window coordinates? For shame... */
pdce->dCtlWindow=OpenWindow("\POur Window",100,50,300,150);
((WindowPeek)pdce->dCtlWindow)->windowKind=pdce->dCtlRefNum;
DrawWindow(pdce->dCtlWindow,"\PSomething funny.");
return 0;
} /* DAOpen */
short
DAClose(piopb,pdce)
CntrlParam *piopb;
DCtlPtr pdce;
{
if ( pdce->dCtlWindow != NULL )
KillWindow ( pdce->dCtlWindow );
if ( pProc != NULL )
UnloadProc ( pProc );
}
Boolean
FLoadProc ( subid, ppProc )
short subid;
Ptr *ppProc;
{
Handle hProc;
hProc = GetResource ( 'PROC', rsidBase + subid );
if ( hProc == NULL ) {
*ppProc = NULL;
return FALSE;
}
/* avoid fragmentation and other evils */
MoveHHi ( hProc );
HLock ( hProc );
HNoPurge ( hProc );
*ppProc = *hProc;
return TRUE;
}
void
UnloadProc ( pProc )
Ptr pProc;
{
Handle hProc;
if ( pProc == NULL )
return;
hProc = RecoverHandle ( pProc );
if ( hProc == NULL )
return;
HUnlock ( hProc );
HPurge ( hProc );
return;
}
WindowPtr
OpenWindow ( stTitle, xLeft, yTop, xRight, yBottom )
char stTitle[];
short xLeft, yTop, xRight, yBottom;
{
JumpToProc(pProc,irtnOpenWindow,6);
}
void
DrawWindow(pwind,st)
WindowPtr pwind;
char st[];
{
JumpToProc(pProc,irtnDrawWindow,4);
}
void
KillWindow(pwind)
WindowPtr pwind;
{
JumpToProc(pProc,irtnKillWindow,2);
}